/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.view.action;

import cn.txdnet.txcms.dict.BooleanType;
import cn.txdnet.txcms.dict.ChannelType;
import cn.txdnet.txcms.dict.InfoType;
import cn.txdnet.txcms.domain.Catalogs;
import cn.txdnet.txcms.domain.Infobase;
import cn.txdnet.txcms.domain.Tagslib;
import cn.txdnet.txcms.dto.InfoDTO;
import cn.txdnet.txcms.dto.PageDTO;
import cn.txdnet.txcms.query.ViewInfoQuery;
import cn.txdnet.txcms.service.CatalogsService;
import cn.txdnet.txcms.service.InfobaseService;
import cn.txdnet.txcms.service.TagslibService;
import cn.txdnet.txweb.annotation.methods.GetMethod;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.JsonUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 * 用于信息内容前台展示
 *
 * @author txdnet
 */
public class InfoAction {

    public ModelView all(HttpServletRequest req) {
        int listsize = WebUtil.getInt(req, "ls", 40);
        int pageno = WebUtil.getInt(req, "p", 1);
        int crow = (pageno - 1) * listsize;
        PageDTO pageDto = this.getCatInfoList4Display(0, crow, listsize,
                InfoType.TP.getValue(), BooleanType.TRUE.getValue());
        Map<String, Object> extdata = new HashMap<String, Object>(3);
        int rsNum = pageDto.getList().size();
        // 下一页
        extdata.put("nextpage", rsNum < listsize ? null : (pageno + 1));
        // 上一页
        extdata.put("prevpage", pageno < 2 ? null : (pageno - 1));
        extdata.put("pageno", pageno);
        pageDto.setExtdata(extdata);
        return ModelUtil.getJspModel("all", pageDto);
    }

    /**
     * 简单的info数据页面展示
     *
     * @param req
     * @return
     */
    public ModelView view(HttpServletRequest req) {
        long infoid = WebUtil.getLong(req, "id", -1);
        if (infoid < 0) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        Infobase info = InfobaseService.queryInfobaseForView(infoid);
        if (info == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJspModel("base_view", info);
    }

    /**
     * 信息浏览次数更新
     *
     * @param req
     * @return
     */
    @GetMethod
    public void hit_num(HttpServletRequest req) {
        long infoid = WebUtil.getLong(req, "id", -1);
        if (infoid < 0) {
            return;
        }
        InfobaseService.increaseHitNum(infoid);
    }

    /**
     * 信息列表页
     *
     * @param req
     * @return
     */
    public ModelView list(HttpServletRequest req) {
        int cid = WebUtil.getInt(req, "cid", -1);
        if (cid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Catalogs cat = CatalogsService.getCatalogsByCid(cid);
        if (cat == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        // 不是资讯版块不得展现页面
        if (cat.getInfotype().byteValue() != ChannelType.INFO.getValue()) {
            return ModelUtil.getTextModel(Message.MSG_VALID_ERR);
        }
        return ModelUtil.getJspModel("base_list", cat);
    }

    /**
     * info tag list page
     *
     * @param req
     * @return
     */
    public ModelView tags(HttpServletRequest req) {
        int tagid = WebUtil.getInt(req, "id", -1);
        if (tagid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Tagslib tag = TagslibService.queryByTagid(tagid);
        if (tag == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJspModel("tags", tag);
    }

    /**
     * tag页面信息列表输出
     *
     * @param req
     * @return
     */
    public ModelView outTagInfoList(HttpServletRequest req) {
        int tagid = WebUtil.getInt(req, "tagid", -1);
        int crow = WebUtil.getInt(req, "cr", 0);
        int listsize = WebUtil.getByte(req, "ls", 12);
        byte infotype = WebUtil.getByte(req, "type", -1);
        String jsonpcall = WebUtil.getNoNull(req, "jsonpcall", null);
        if (jsonpcall == null || tagid <= 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        return ModelUtil.getJsModel(this.parseJsonpStr(jsonpcall,
                this.getTagsInfoList4Display(tagid, crow, listsize, infotype,
                BooleanType.TRUE.getValue())));
    }

    /**
     * 类目信息列表
     *
     * @param req
     * @return
     */
    public ModelView outCatInfoList(HttpServletRequest req) {
        int cid = WebUtil.getInt(req, "cid", -1);
        int crow = WebUtil.getInt(req, "cr", 0);
        int listsize = WebUtil.getByte(req, "ls", 12);
        byte infotype = WebUtil.getByte(req, "type", -1);
        String jsonpcall = WebUtil.getNoNull(req, "jsonpcall", null);
        if (jsonpcall == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        return ModelUtil.getJsModel(this.parseJsonpStr(jsonpcall,
                this.getCatInfoList4Display(cid, crow, listsize, infotype,
                BooleanType.TRUE.getValue())));
    }

    /**
     * info 区域性栏目列表
     *
     * @param req
     * @return
     */
    public ModelView outInfoList(HttpServletRequest req) {
        int cid = WebUtil.getInt(req, "cid", -1);
        int crow = WebUtil.getByte(req, "cr", 0);
        int listsize = WebUtil.getByte(req, "ls", 12);
        byte infotype = WebUtil.getByte(req, "type", -1);
        String jsonpcall = WebUtil.getNoNull(req, "jsonpcall", null);
        byte descByHitnum = WebUtil.getByte(req, "descByHit", 0);
        if (jsonpcall == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        return ModelUtil.getJsModel(this.parseJsonpStr(jsonpcall,
                this.getCatInfoList4Aside(cid, crow, listsize, infotype, descByHitnum)));
    }

    /**
     * 查询栏目列表信息
     *
     * @param cid
     * @param crow
     * @param listsize
     * @return
     */
    private List<Infobase> getCatInfoList4Aside(int cid, int crow, int listsize, byte infotype, byte orderByHitnum) {
        ViewInfoQuery query = new ViewInfoQuery();
        if (cid > 0) {
            query.setCid(cid);
        }
        query.setCrow(crow);
        query.setListsize(listsize);
        query.setOrderByHitnum(orderByHitnum);
        if (infotype >= 0) {
            query.setInfotype(infotype);
        }
        return InfobaseService.listByFactorForView(query);
    }

    /**
     * 用于cat list 阶段加载列表
     *
     * @param cid
     * @param crow
     * @param listsize
     * @param infotype
     * @return
     */
    private PageDTO getCatInfoList4Display(int cid, int crow, int listsize, byte infotype, byte useSummary) {
        ViewInfoQuery query = new ViewInfoQuery();
        if (cid > 0) {
            query.setCid(cid);
        }
        query.setCrow(crow);
        query.setListsize(listsize);
        query.setUseSummary(useSummary);
        if (infotype >= 0) {
            query.setInfotype(infotype);
        }
        List<InfoDTO> infoDtoList = InfobaseService.listInfoForView(query);
        return new PageDTO(infoDtoList, infoDtoList.size());

    }

    /**
     * 前台tag信息列表显示
     *
     * @param tagid
     * @param crow
     * @param listsize
     * @param infotype
     * @param useSummary
     * @return
     */
    private PageDTO getTagsInfoList4Display(int tagid, int crow, int listsize, byte infotype, byte useSummary) {
        ViewInfoQuery query = new ViewInfoQuery();
        query.setTagid(tagid);
        query.setCrow(crow);
        query.setListsize(listsize);
        query.setUseSummary(useSummary);
        if (infotype >= 0) {
            query.setInfotype(infotype);
        }
        List<?> infoList = InfobaseService.listByTagsForView(query);
        return new PageDTO(infoList, infoList.size());
    }

    /**
     * 转换对象为jsonp字符串
     *
     * @param callbackName
     * @param obj
     * @return
     */
    private String parseJsonpStr(String callbackName, Object obj) {
        return callbackName.concat("(").concat(JsonUtil.toJSONString(obj)).concat(")");
    }
}
